﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PraxisRent.Entities;
using System.Data;

namespace PraxisRent.Repository
{
    public class CreditoRepository:MasterRepository,ICreditoRepository
    {

        public List<CREDITO> GetAllFromCredito()
        {
            var query = from c in context.CREDITOs.Include("ALQUILER")
                        select c;
            return query.ToList();
        }

        public List<CREDITO> GetAllFromCreditoByCliente(string criterio)
        {
            var query = from e in context.CREDITOs.Include("ALQUILER")
                        select e;
            if (criterio != null && criterio != string.Empty && criterio != "")
            {
                query = from e in context.CREDITOs.Include("ALQUILER")
                        where e.ALQUILER.CLIENTE.CLIENTE_JURIDICO.RazonSocial.ToUpper().Contains(criterio) ||
                                  e.ALQUILER.CLIENTE.CLIENTE_JURIDICO.Ruc.ToUpper().Contains(criterio) ||
                                  e.ALQUILER.CLIENTE.CLIENTE_NATURAL.ApMaterno.ToUpper().Contains(criterio) ||
                                  e.ALQUILER.CLIENTE.CLIENTE_NATURAL.ApPaterno.ToUpper().Contains(criterio) ||
                                  e.ALQUILER.CLIENTE.CLIENTE_NATURAL.Nombre.ToUpper().Contains(criterio) ||
                                  e.ALQUILER.CLIENTE.CLIENTE_NATURAL.Dni.ToUpper().Contains(criterio)
                        select e;
            }
            return query.ToList();
        }

        public List<CREDITO> GetAllFromCreditoByAlquiler(int codAlquiler)
        {
            var query = from e in context.CREDITOs.Include("ALQUILER")
                        select e;
            if (codAlquiler != null)
            {
                query = from e in context.CREDITOs.Include("ALQUILER")
                        where e.CodigoAlquiler.Equals(codAlquiler)
                        select e;
            }
            return query.ToList();
        }

        public CREDITO GetAllFromCreditoById(int id)
        {
            var query = from e in context.CREDITOs.Include("ALQUILER")
                        where e.Id.Equals(id)
                        select e;
            return query.SingleOrDefault();
        }

        public void AddCredito(CREDITO credito)
        {
            credito.ALQUILER = null;
            
            context.CREDITOs.Add(credito);
            context.SaveChanges();
        }

        public void UpdateCredito(CREDITO credito)
        {
            credito.ALQUILER = null;

            context.Entry(credito).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}
